<?php
namespace app\index\controller;
use \think\Controller;

class Api extends Controller
{
    protected $baseURL = 'http://service.envicloud.cn:8082';
    protected $accesskey = 'ZNHWAXHLBHMXNTAYOTM1NJUXNJI0';
    protected function curl($url){
        header('Access-Control-Allow-Origin:*');  
        $curl = curl_init();
        
        curl_setopt_array($curl, array(
          CURLOPT_PORT => "8082",
          CURLOPT_URL => $url,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_POSTFIELDS => "",
        ));
        
        $response = curl_exec($curl);
        $err = curl_error($curl);
        
        curl_close($curl);
        
        if ($err) {
          return "cURL Error #:" . $err;
        } else {
          return $response;
        }
    }

    public function weatherforecast($citycode)
    {
      $url = $this->baseURL.'/v2/weatherforecast/'.$this->accesskey.'/'.$citycode;
      $res = $this->curl($url);
      return $res;
    }

    public function citycode($cityname)
    {
      $url = $this->baseURL.'/v2/citycode/'.$this->accesskey.'/'.$cityname;
      $res = $this->curl($url);
      return $res;
    }

    public function cityairhistory($citycode)
    {
      $url = $this->baseURL.'/v2/cityairhistory/'.$this->accesskey.'/'.$citycode;
      $res = $this->curl($url);
      return $res;
    }

    public function cityairlive($citycode, $type = '')
    {
      $url = $this->baseURL.'/v2/cityairlive/'.$this->accesskey.'/'.$citycode.'/'.$type;
      $res = $this->curl($url);
      return $res;
    }

}
